<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<title>生成拣货任务</title>
		<script type="text/javascript">
	var batchCode = "";
	var count = 0;
	$(function() {
		$("#inputForm").validate(); //为inputForm注册validate函数
		$("#beginTimeStr").datepicker({
			format : 'yyyy-mm-dd'
		});
		$("#endTimeStr").datepicker({
			format : 'yyyy-mm-dd'
		});

	});

	function ajaxCreateBackupTask() {
	var groups=getGroups();
		showBg();
		$.ajax({
			type : 'post',
			url : '${ctx}/backupTask/ajaxCreateBackupTask',
			data : 'batchTaskCode=' + batchCode + '&groups=' + groups,
			dataType : 'json',
			success : function(data) {
				$("#message").show();
				if (data.status == "1") {
					$("#content").html("生成拣货任务个数:" + data.content);
					window.location.reload(true);
				} else {
					$("#content").html(data.content);
				}
				closeBg();
			},
			error : function() {
				alert("获取信息错误,打印失败！");
			}
		});
	}

	$(function() {
		$("#beginTimeStr").datepicker().on(
				'changeDate',
				function() {
					$("#inputForm").attr("action",
							"${ctx}/batchTask/batchTaskLists/" + this.value);
					$("#inputForm").submit();
				});
	});

	function getGroups() {
		var groups = "";
		for ( var i = 0; i < count; i++) {
			var group = $("#number_" + i).val();
			if (groups == "") {
				groups = group;
			} else {
				groups += "," + group;
			}
		}
		return groups;
	}

	function ajaxGroupBackupTask(item) {
		batchCode = item;
		$
				.ajax({
					type : 'post',
					url : '${ctx}/backupTask/ajaxGroupBackupTask',
					data : 'batchTaskCode=' + item + '&number='
							+ $("#" + item + "number").val(),
					dataType : 'json',
					success : function(data) {
						if (data.status == "1") {
							var tempString = "";
							count=0;
							$.each(data.content,function(i) {
												count++;
												tempString += "<tr><td>"
														+ data.content[i].groupOrdenos
														+ "</td><td>";
												tempString += "<select id=\"number_"+i+"\" class=\"input-medium\">";
												tempString +="<option value=\""+data.content[i].groupOrdenos+":-1\">不指派</option>";
												$.each(data.content[i].userDTOs,function(j) {
																	tempString += "<option value="+data.content[i].groupOrdenos+":"+data.content[i].userDTOs[j].jobuuid+">"
																			+ data.content[i].userDTOs[j].username
																	"</option>";
																});
												tempString += "</select></td></tr>";
											});
							$("#tbodyContent").html(tempString);
							$("#tbodyContent").trigger("chosen:updated");
						} else {
							$("#content").html(data.content);
						}
					},
					error : function() {
						alert("获取信息错误,打印失败！");
					}
				});
	}
</script>


	</head>

	<body>
		<div id="fullbg"></div>
		<div id="dialog">
			<div>
				<img src="${ctx }/static/022.gif"></img>
			</div>
		</div>
		<div id="message" class="alert alert-success" style="display: none;">
			<span id="content"></span>
		</div>
		<div id="fullbg"></div>
		<div id="dialog">
			<div>
				<img src="${ctx }/static/022.gif"></img>
			</div>
		</div>
		<div class="row">
			<form method="post" id="inputForm" class="form-search">
				<label>
					时间:
				</label>
				<input type="text" id="beginTimeStr" value="${from}"
					class="input-large required" />
			</form>
		</div>
		<table id="contentTable"
			class="table table-striped table-bordered table-condensed">
			<thead>
				<tr>
					<th>
						编号
					</th>
					<th>
						生成日期
					</th>
					
					<th>
						订单数量
					</th>
					<th>
						商品数量
					</th>
					<th>
						分拣类别
					</th>
					<th>
						操作人
					</th>
					<th>
						任务订单量
					</th>
					<th>
						批次类型
					</th>
					<th>
						网点
					</th>
					<th>
						状态
					</th>
					<th>
						操作
					</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach items="${object}" var="obj">
					<tr>
						<td>
							${obj.batchTaskCode }
						</td>
						<td>
							${obj.createDate }
						</td>
						
						<td>
							${obj.ordenoQty }
						</td>
						<td>
							${obj.skuQty }
						</td>

						<td>
							<c:if test="${obj.type eq 'single' }">
							     单件
							</c:if>
							<c:if test="${obj.type eq 'multiple' }">
							  多件
							</c:if>
							<c:if test="${obj.type eq 'forward' }">
							  中转发
							</c:if>
						</td>
						<td>
							${obj.username }
						</td>
						<td>
							<select name="number" class="input-medium"
								id="${obj.batchTaskCode}number">
								<c:forEach items="${obj.backupTaskNumber }" var="o">
								<option value="${o }">
									${o }
								</option>
							</c:forEach>
							</select>
						</td>
						
						<td>
							<c:if test="${obj.batchType eq 'zwarehouse'}">
							         <span class="label label-success">总仓订单</span>
							</c:if>
							<c:if test="${obj.batchType eq 'fwarehouse'}">
							       <span class="label label-important">分仓订单</span>
							</c:if>
						</td>
						<td>
						 ${obj.areaGroupName }
						</td>
						<td>
							<c:if test="${obj.status eq 'finish' }">
							      已分配
							</c:if>
							<c:if test="${obj.status eq 'pending' }">
								未分配
							</c:if>
						</td>
						<td>
						   <c:if test="${obj.status eq 'pending' }">
								<a href="#backupTaskSelect" role="button" class="btn btn-success"
								data-toggle="modal"
								onclick="ajaxGroupBackupTask('${obj.batchTaskCode}')"> 生成拣货任务
							</a>
							</c:if>
							
						</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>

		<div id="backupTaskSelect" class="modal hide fade" tabindex="-1"
			role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-header">
				<h3>
					请分配拣货任务
				</h3>
			</div>
			<div class="modal-body">
				<table class="table table-striped table-bordered table-condensed">
					<thead>
						<tr>
							<th>
								订单
							</th>
							<th>
								用户
							</th>

						</tr>
					</thead>
					<tbody id="tbodyContent">
					</tbody>
				</table>
			</div>
			<div class="modal-footer">
				<button class="btn btn-success" data-dismiss="modal" aria-hidden="true"
					onclick="ajaxCreateBackupTask();">
					确定分配
				</button>
			</div>
		</div>
	</body>
</html>
